package com.qbkj.base_lib.adapter;


import android.util.SparseArray;

import java.lang.reflect.Field;
import java.net.HttpURLConnection;

/**
 * Created by Administrator on 2017/5/18 0018.
 */

public class ReflectField {

    private static ReflectField mReflectField;
    private SparseArray<SparseArray<Field>> mFieldCache;
    private SparseArray<Field> mFields;

    public static ReflectField getInstance() {
        if (mReflectField == null) {
            synchronized (ReflectField.class) {
                if (mReflectField == null) {
                    mReflectField = new ReflectField();
                }
            }

        }
        return mReflectField;
    }

    private ReflectField() {
        mFields = new SparseArray<>();
        mFieldCache = new SparseArray<>();
    }

    private final String getFieldSubclass(Object object, String[] fields) throws NoSuchFieldException, IllegalAccessException {
        for (int i = 0; i < fields.length; i++) {
            if (object == null) return null;
            Field field = object.getClass().getDeclaredField(fields[i]);
            field.setAccessible(true);
            if (isBasicType(field)) return field.get(object) + "";
            else object = field.get(object);
        }
        return null;
    }

    public void upSingleData(int position) {
        mFieldCache.get(position).clear();
    }


    public final String getFieldName(int position, int index, Object object, String tag) {
        try {
            if (tag.contains(".")) {
                String[] fields = tag.split("\\.");
                return getFieldSubclass(object, fields);
            }
            Field field = null;
            try {
                field = mFieldCache.get(position).get(index);
                if (isBasicType(field)) {
                    return field.get(object) + "";
                }
            } catch (NullPointerException e) {
                field = object.getClass().getDeclaredField(tag);
                field.setAccessible(true);
                if (isBasicType(field)) {
                    return field.get(object) + "";
                }
                mFields.put(index, field);
                mFieldCache.put(position, mFields);
            } finally {

            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    private boolean isBasicType(Field field) {
        if (field.getType().getName().equals(String.class.getName())) return true;
        if (field.getType().getName().equals(Integer.class.getName()) || field.getType().getName().equals("int"))
            return true;
        if (field.getType().getName().equals(Float.class.getName()) || field.getType().getName().equals("float"))
            return true;
        if (field.getType().getName().equals(Long.class.getName()) || field.getType().getName().equals("long"))
            return true;
        if (field.getType().getName().equals(Double.class.getName()) || field.getType().getName().equals("double"))
            return true;
        if (field.getType().getName().equals(Byte.class.getName()) || field.getType().getName().equals("byte"))
            return true;
        if (field.getType().getName().equals(Short.class.getName()) || field.getType().getName().equals("short"))
            return true;
        if (field.getType().getName().equals(Boolean.class.getName()) || field.getType().getName().equals("boolean"))
            return true;
        if (field.getType().getName().equals(Character.class.getName()) || field.getType().getName().equals("char"))
            return true;
        return false;
    }
}
